<?php

class User{

	public $userId;
	public $username;
	public $name;
	public $password = "NULL";
	public $email = "NULL";
	public $avatar = "NULL";
	public $admin = 0;
	public $regDate = "CURDATE()";
	
	function verificar_email($email){
		$mail_correcto = 0;
		//verifico umas coisas
		if ((strlen($email) >= 6) && (substr_count($email,"@") == 1) && (substr($email,0,1) != "@") && (substr($email,strlen($email)-1,1) != "@")){
			if ((!strstr($email,"'")) && (!strstr($email,"\"")) && (!strstr($email,"\\")) && (!strstr($email,"\$")) && (!strstr($email," "))) {
				//vejo se tem caracter .
				if (substr_count($email,".")>= 1){
					//obtenho a terminação do dominio
					$term_dom = substr(strrchr ($email, '.'),1);
					//verifico que a terminação do dominio seja correcta
					if (strlen($term_dom)>1 && strlen($term_dom)<5 && (!strstr($term_dom,"@")) ){
						//verifico que o de antes do dominio seja correcto
						$antes_dom = substr($email,0,strlen($email) - strlen($term_dom) - 1);
						$caracter_ult = substr($antes_dom,strlen($antes_dom)-1,1);
						if ($caracter_ult != "@" && $caracter_ult != "."){
							$mail_correcto = 1;
						}
					}
				}
			}
		}
		if ($mail_correcto) return 1; else return 0;
	}	
		
	function registrar($name, $username, $pass, $email){
		
		$q 			= "SELECT * FROM user WHERE username='$username' or email='$email'";
		$query 		= mysql_query("$q") or die(mysql_error());
		$numrows	=mysql_numrows($query);
		
		if($numrows!=0){
		$query 		= mysql_fetch_array($query, MYSQL_BOTH);
		$q_user 	= strtolower($query['username']);
		$q_mail 	= strtolower($query['email']);
		$l_user 	= strtolower($username);
		$l_mail 	= strtolower($mail);
		}
	
		if($q_mail == $l_mail){
			echo("E-mail j&aacute; utilizado.<br />");
		}
		if($q_user == $l_user){
			echo("Usu&aacute;rio j&aacute; existente.<br />");
		}
		if(($q_user != $l_user) and ($q_mail != $l_mail)){
			$q = "INSERT INTO user(name,username,password,email,avatar,admin,regDate) VALUES('$name','$user','$pass','$mail','$avatar','$admin','$regDate');";
			$query = mysql_query("$q") or die(mysql_error());
			return true;
		}
	}
	
	function getUserId($user){//Retorna o ID do usuário
		$q = "SELECT idUser FROM user WHERE UserName='$user'";
		$query = mysql_query("$q") or die(mysql_error());
		$query = mysql_fetch_array($query, MYSQL_BOTH);
		$ret = $query['idUser'];
		return $ret;
	}
	
	function getUserName($id){//Retorna o nome do usuário
		$q = "SELECT name FROM user WHERE idUser='$id'";
		$query = mysql_query("$q") or die(mysql_error());
		$query = mysql_fetch_array($query, MYSQL_BOTH);
		$ret = $query['name'];
		return $ret;
	}
	
	function getUserAvatar($id){//Retorna o nome do usuário
		$q = "SELECT avatar FROM user WHERE idUser='$id'";
		$query = mysql_query("$q") or die(mysql_error());
		$query = mysql_fetch_array($query, MYSQL_BOTH);
		$ret = $query['avatar'];
		return $ret;
	}
	
	function getUserUserName($id){//Retorna o nome do usuário
		$q = "SELECT username FROM user WHERE idUser='$id'";
		$query = mysql_query("$q") or die(mysql_error());
		$query = mysql_fetch_array($query, MYSQL_BOTH);
		$ret = $query['username'];
		return $ret;
	}
	
	function getUserEmail($id){//Retorna o E-Mail do usuário
		$q = "SELECT email FROM user WHERE idUser='$id'";
		$query = mysql_query("$q") or die(mysql_error());
		$query = mysql_fetch_array($query, MYSQL_BOTH);
		$ret = $query['email'];	
		return $ret;
	}
	
	function isAdmin($id){// Verifica se o usuário é administrador
		$q = "SELECT admin FROM user WHERE idUser='$id'";
		$query = mysql_query("$q") or die(mysql_error());
		$query = mysql_fetch_array($query, MYSQL_BOTH);
		$ret = $query['admin'];
		if($ret==1){return TRUE;}else{return FALSE;}
	}
	
	function isLogged($user){//Verifica se o usu�rio est� logado.
		$q = "SELECT username FROM user WHERE username='$user'";
		$query = mysql_query("$q") or die(mysql_error());
		$query = mysql_fetch_array($query, MYSQL_BOTH);
		$userLower = strtolower($query['username']);
		$userSession = strtolower($user);
		if($userLower==$userSession){return TRUE;}else{return FALSE;}
	}
	
	function editPerfil($name,$avatar,$email,$id){
		$q = "UPDATE user SET Name='$name',Email='$email',avatar='$avatar' WHERE idUser='$id';";
		$query = mysql_query("$q") or die(mysql_error());
		header("location: /home/p/home");
	}	
}
?>